﻿// Decompiled with JetBrains decompiler
// Type: MCM.Common.ProxyRef`1
// Assembly: MCMv5, Version=5.10.2.0, Culture=neutral, PublicKeyToken=null
// MVID: 7AB326B9-B2B4-41C2-9177-68E944071101
// Assembly location: D:\steam\steamapps\workshop\content\261550\2859238197\bin\Win64_Shipping_Client\MCMv5.dll

using System;
using System.ComponentModel;
using System.Runtime.CompilerServices;

#nullable enable
namespace MCM.Common
{
  public class ProxyRef<T> : IRef, INotifyPropertyChanged, IEquatable<ProxyRef<T>>
  {
    private readonly Func<T> _getter;
    private readonly Action<T>? _setter;

    public event PropertyChangedEventHandler? PropertyChanged;

    public Type Type => typeof (T);

    public object? Value
    {
      get => (object) this._getter();
      set
      {
        if (this._setter == null || !(value is T obj))
          return;
        this._setter(obj);
        this.OnPropertyChanged(nameof (Value));
      }
    }

    public ProxyRef(Func<T> getter, Action<T>? setter)
    {
      this._getter = getter ?? throw new ArgumentNullException(nameof (getter));
      this._setter = setter;
    }

    protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
    {
      PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
      if (propertyChanged == null)
        return;
      propertyChanged((object) this, new PropertyChangedEventArgs(propertyName));
    }

    public bool Equals(ProxyRef<T>? other)
    {
      if ((object) other == null)
        return false;
      return (object) this == (object) other || this._getter.Equals((object) other._getter) && object.Equals((object) this._setter, (object) other._setter);
    }

    public override bool Equals(object? obj)
    {
      if (obj == null)
        return false;
      if ((object) this == obj)
        return true;
      return !(obj.GetType() != this.GetType()) && this.Equals((ProxyRef<T>) obj);
    }

    public override int GetHashCode()
    {
      int hashCode = 269 * 47 + this._getter.GetHashCode();
      if (this._setter != null)
        hashCode = hashCode * 47 + this._setter.GetHashCode();
      return hashCode;
    }

    public static bool operator ==(ProxyRef<T>? left, ProxyRef<T>? right)
    {
      return object.Equals((object) left, (object) right);
    }

    public static bool operator !=(ProxyRef<T>? left, ProxyRef<T>? right)
    {
      return !object.Equals((object) left, (object) right);
    }
  }
}
